package com.huatai.model.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huatai.common.api.R;
import com.huatai.model.config.TreeNodeConfig;
import com.huatai.model.entity.DictionaryEntity;
import com.huatai.model.entity.TreeNodeMap;
import com.huatai.model.service.DictionaryService;
import com.huatai.model.utils.TreeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Lion
 * @date 2024/4/7  13:28
 */
@RestController
@Api(tags = "标签管理")
public class LabelController {

	@Autowired
	private DictionaryService treeService;

	@RequestMapping("/getMenuInfo")
	@ApiOperation(value = "获取菜单树", notes = "获取菜单树")
	public R getTreeNodes2(){

		List<DictionaryEntity> menuEntityList = treeService.list();
		TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
		List<TreeNodeMap> treeNodes = TreeUtil.build(menuEntityList, "0",treeNodeConfig,new TreeUtil.Convert<DictionaryEntity, TreeNodeMap>() {
			@Override
			public void convert(DictionaryEntity object, TreeNodeMap treeNode) {
				treeNode.setId(object.getId().toString());
				treeNode.setParentId(object.getPid());
				//treeNode.setCode(object.getCode());
				treeNode.setTitle(object.getConfigName());
				treeNode.setType(object.getConfigKey());
			}
		});
		JSONObject data = new JSONObject();

		for(TreeNodeMap node:treeNodes){
			if(node.getType().equals("modelLabel")){
				data.put("tag",node.getChildren());
			}else if (node.getType().equals("unit")){
				for(TreeNodeMap unit : node.getChildren()){
					data.put(unit.getType(),unit.getChildren());
				}
			}
		}

		return R.data(data);
	}

}
